package com.petmall.service;

import com.petmall.entity.*;
import com.petmall.mapper.UserMapper;
import com.petmall.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by pig2guang on 2017/4/3.
 */
@Service
public class UserService {

    @Autowired
    private UserMapper usermapper;

    public User selectById(long id){
        return usermapper.selectById(id);
    }

    public User selectByIdObj(Object user){
        return usermapper.selectById(new UserUtil().getUserId(user));
    }

    public User selectByPassword(String username, String password){
        return usermapper.selectByPassword(username, password);
    }

//    public User selectByUsername(String username){
//        return usermapper.selectByUsername(username);
//    }

    public User insertUser(String phone, String password){
        User isRegistered = usermapper.selectByPhone(phone);
        if( isRegistered == null ){
            String username = phone;  // 手机号注册时默认用户名为手机号
            String head_portrait = "headportrait/h_normal.jpg";  // 注册时默认头像
            String level = "铜牌会员";  // 新注册用户默认为铜牌会员
            usermapper.insertUser(username, password, phone, head_portrait, level);
            return usermapper.selectByPhone(phone);
        }else{
            return null;
        }

    }

    public void updateUserInfo(User user, Object obj_user){
        usermapper.updateUserByUserId(new UserUtil().getUserId(obj_user), user.getUsername(), user.getEmail(), user.getRealname(), user.getSex(), user.getBirthday());
    }

    public void updateUserLevelAndGold(long user_id, int level_value_add, int gold_add){
        int level_value = usermapper.selectById(user_id).getLevel_value() + level_value_add;
        for(Level level_info : new UserUtil().getLevelsInfo()){
            if(level_value >= level_info.getLevel_point()){
                usermapper.updateLevelGold(user_id, level_info.getLevel_name(), level_value_add, gold_add);
                break;
            }
        }
    }

    //增加一条金币信息变动详情
    public void addUserGoldDetail(long user_id, int change, String detail, String time){
        //usermapper.insertGoldDetail(user_id,change,detail,time);
        //System.out.println("user_id:" + user_id + ",change:" + change + ",detail:" + detail + ",time:" + time );
    }

    //增加一条积分信息变动详情
    public void addUserLevelDetail(long user_id, int change, String detail, String time){
        //usermapper.insertLevelDetail(user_id,change,detail,time);
        //System.out.println("user_id:" + user_id + ",change:" + change + ",detail:" + detail + ",time:" + time );
    }

    public void addMd5User(User_md5 user){
        usermapper.insertMd5User(user.getUsername(), user.getPassword());
    }

    public Boolean findMd5User(User_md5 user){
        User_md5 is_user = usermapper.selectMd5User(user.getUsername(), user.getPassword());
        if(is_user == null){
            return false;
        }else {
            return true;
        }
    }

    public Boolean isExistMd5User(User_md5 user){
        User_md5 isExist_user = usermapper.selectMd5UserByUserName(user.getUsername());
        if(isExist_user == null){
            return false;
        }else {
            return true;
        }
    }

}
